# SPDX-License-Identifier: Apache-2.0
# Author: Robert Balas (balasr@iis.ee.ethz.ch)

TOOLCHAIN	= /opt/riscv-none-embed/bin
PREFIX		= $(TOOLCHAIN)/riscv-none-embed-

CFLAGS		= -march=rv32im -mabi=ilp32 -Os -g
LDFLAGS         = -nostdlib -static -T sw/link.ld
LDLIBS		= -lc -lm -lgcc

default: tb_sim

firmware: sw/hello_world.hex
sw/hello_world: sw/hello_world.c sw/crt0.S sw/syscalls.c sw/vectors.S
	$(PREFIX)gcc $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@

%.hex: %
	$(PREFIX)objcopy -F elf32-littleriscv -O verilog $< $@

CATV_DIR = ../src
CATV_RTL = $(CATV_DIR)/catv_riscv.sv

catv_tb.sv: firmware
tb_sim: catv_tb.sv $(CATV_RTL)
	iverilog -g2005-sv -I $(CATV_DIR) -o tb.vvp catv_tb.sv $(CATV_RTL) 
	vvp tb.vvp -lxt2 +elf=./sw/hello_world.hex

.PHONY: clean
clean:
	@rm -f sw/hello_world sw/hello_world.hex
	@rm -rf *.hex *.vvp *.lxt*
